package com.service.user.feignService.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.client.model.pojo.user.ArUser;
import com.client.model.vo.common.Result;
import com.client.model.vo.common.ResultCodeEnum;
import com.client.model.vo.userVo.ArUserVo;
import com.service.user.feignService.UserFeignService;
import com.service.user.mapper.ArUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 调用用户的信息，来匹配当前用户是否输入了正确的登录信息
 *
 * @Author : RulerWood
 * @Data : 2024/4/2 - 21:06
 */
@Slf4j
@Service
public class UserFeignServiceImpl implements UserFeignService {
    @Autowired
    private ArUserMapper arUserMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 该方法可以
     *
     * @param arUserVo
     * @return
     */
    @Override
    public Result<String> theUserIsExist(ArUserVo arUserVo) {
        log.info("arUserVo------------>{}", arUserVo);
        //判断当前传入的用户信息是否符合
        if (ObjectUtil.isEmpty(arUserVo)) {
            throw new RuntimeException("用户信息不能为空");
        }
        if (StringUtils.isEmpty(arUserVo.getMobile())) {
            throw new RuntimeException("手机号不能为空");
        }
        if (StringUtils.isEmpty(arUserVo.getCode())) {
            throw new RuntimeException("验证码不能为空");
        }
        if (StringUtils.isEmpty(arUserVo.getRealName())) {
            throw new RuntimeException("真实姓名不能为空");
        }
        //判断手机号所发送的验证码是否正确
        String codeKey = this.redisTemplate.opsForValue().get(arUserVo.getMobile() + ":" + arUserVo.getToken());
        assert codeKey != null;
        if (!codeKey.equals(arUserVo.getCode())) {
            //如果redis存放的验证码与用户输入过来的不匹配则证明用户输入错误
            return Result.build(null, ResultCodeEnum.VALIDATECODE_ERROR);
        }
        //检验其它信息
        QueryWrapper<ArUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("real_name", arUserVo.getRealName());
        queryWrapper.eq("iden", arUserVo.getIden());
        ArUser arUser = this.arUserMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(arUser)) {
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
        return Result.build("验证通过", ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result<ArUser> getUserById(Long id) {
        return Result.build(this.arUserMapper.selectById(id), ResultCodeEnum.SUCCESS);
    }
}
